package com.superm.controller;

import com.superm.entity.Emp;
import com.superm.entity.Member;
import com.superm.entity.Order;
import com.superm.entity.SaleOrder;
import com.superm.mappers.OrderMapper;
import com.superm.mappers.SaleOrderDetailsMapper;
import com.superm.service.OrderService;
import com.superm.service.SaleODetService;
import com.superm.service.SaleOrderService;
import com.superm.utils.EmpContent;
import com.superm.utils.Tool;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/saleOrder")
public class SaleOrderController {
    @Autowired
    private SaleOrderService saleOrderService;
    @Autowired
    private OrderService orderService;
    @RequestMapping(path = "/addSaleOrder")
    @ResponseBody
    public Map<String,Object> addSaleOrder(@RequestParam("ords[]") List<Integer> ords,@RequestParam("paymanner")String paymanner,@RequestParam(required = false)Member member){
        System.out.println(ords);
        System.out.println(paymanner);
        System.out.println(member);
        return saleOrderService.addSaleOrder(ords,paymanner,member);
    }
    @RequestMapping(path = "/MemberSaleOrder")
    public ModelAndView MemberSaleOrder(String ordsStr){
        ModelAndView ma = new ModelAndView("members/adminMember");
        Emp emp = EmpContent.EmpUtils();
        //应付金额
        double goodsYPrice=0f;
        List<Order> orders = orderService.allGoodsids(ordsStr,emp.geteId());
        for (Order o:orders) {
            goodsYPrice+=o.getGoodstotlePrice();
        }
        ma.addObject("ordsStr",ordsStr);
        ma.addObject("goodsYPrice",goodsYPrice);
        return ma;
    }
    @RequestMapping(path = "/index")
    public String index(){
        return "saleOrder/allSaleOrder";
    }
    @RequestMapping(path = "/allSaleOrder")
    @ResponseBody
    public Map<String, Object> allSaleOrder(String saleOrderCode, @RequestParam("page")Integer pageNo,@RequestParam("limit")Integer pageSize){
        //Emp emp =EmpContent.EmpUtils();
        Map<String, Object> map = new HashMap<>();
        List<SaleOrder> saleOrders =saleOrderService.allSaleOrder(saleOrderCode,pageNo,pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("count",saleOrderService.saleOrderCount(saleOrderCode));
        map.put("data",saleOrders);
        return map;
    }
}
